package com.sander.guardmanagement.base;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;

/**
 * Created by sander on 2017/4/7.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected static UIHandler handler = new UIHandler(Looper.getMainLooper());

    public BaseActivity() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setBase();
        this.setHandler();
        this.initContentView(savedInstanceState);
    }

    // 可能全屏或者没有ActionBar等
    private void setBase(){
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    //让子类处理消息
    protected abstract void handler(Message msg);
    // 初始化UI，setContentView等
    protected abstract void initContentView(Bundle savedInstanceState);

    // 横竖屏切换，键盘等
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        try {
            super.onRestoreInstanceState(savedInstanceState);
        } catch (Exception e) {
        }
    }


    private void setHandler() {
        handler.setHandler(new IHandler() {
            public void handleMessage(Message msg) {
                handler(msg);//有消息就提交给子类实现的方法
            }
        });
    }
}
